/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package com.utils.httpservlet;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @ClassName: HttpUtil
 * @Description: HttpServletResponse,HttpServletRequest处理
 * @author likai
 * @date 2017年5月3日
 *
 */
public class HttpUtil {
	
	/*  
	    * @Description:请求输出文件
	    * 
	    * @param:													是否必须
	    * 
	    * 输出文件：file														是
	    * 输出文件名：fileName												是
	    */

	public static void writeFile(HttpServletResponse response, File file, String fileName) throws IOException {
		InputStream inputStream = new FileInputStream(file);
		byte[] buffer = new byte[inputStream.available()];
		inputStream.read(buffer);
		inputStream.close();
		response.reset();
		response.addHeader("Content-Length", file.length()+"");
		response.addHeader("Content-Disposition",
				"attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
		
		OutputStream os = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("application/octet-stream");
		os.write(buffer);
		os.flush();
		os.close();
	}

	
	/*  
	    * @Description:获取request内容
	    * 
	    * @param:													是否必须
	    * 
	    * 请求：request														是
	    * 内容解码格式：charset												是
	    * 
	    */

	public static String getRequestContent(HttpServletRequest request,String charset) throws IOException {
		String line;
		StringBuffer bufferedString = new StringBuffer();
		InputStream in=request.getInputStream();
		InputStreamReader reader=new InputStreamReader(in, charset);
		BufferedReader buff = new BufferedReader(reader);
        while ((line = buff.readLine()) != null) {
        	bufferedString.append(line);
        	bufferedString.append("\n");
        }
        in.close();
        reader.close();
        buff.close();
        return bufferedString.toString();
	}
	

}

